package com.software.car.common.entity_change;

import com.software.car.entity.dto.UserAdministerUpdateDto;
import com.software.car.entity.dto.UserPersonalUpdateDto;
import com.software.car.entity.po.UserPo;
import com.software.car.entity.vo.UserDetailVo;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

/**
 * 将userPro转换成超级管理员可以查看的Vo
 * 将userPro转换成普通用户可以查看的Vo
 *
 *
 * @Author 魔法はまだ解けない
 * @Date 2021/3/17
 */
@Mapper
public interface UserChangeMapper {

    UserChangeMapper INSTANCE = Mappers.getMapper(UserChangeMapper.class);


    /**
     * 通过用户所有信息映射
     * 用户所有详细信息
     * 实体
     *
     * @param userpo
     * @return
     */
    @Mapping(target = "sexName", expression = "java(changeSex(userpo.getSex()))")
    @Mapping(target = "roleName", expression = "java(changeRole(userpo.getRole()))")
    @Mapping(target = "stateName", expression = "java(changeState(userpo.getState()))")
    UserDetailVo UserPoToUserDetailVo(UserPo userpo);


    /**
     * 通过用户所有信息映射
     * 用户个人信息
     * 实体
     *
     * @param userpo
     * @return
     */
    @Mapping(target = "password", ignore = true)
    @Mapping(target = "sexName", expression = "java(changeSex(userpo.getSex()))")
    @Mapping(target = "roleName", expression = "java(changeRole(userpo.getRole()))")
    @Mapping(target = "stateName", expression = "java(changeState(userpo.getState()))")
    UserDetailVo UserPoToUserPersonalVo(UserPo userpo);

    /**
     * 个人信息修改
     * 用户上传数据转换成统一数据
     * @param userPersonalUpdateDto
     * @param id
     * @return
     */
    @Mapping(target = "id",source = "id")
    UserPo userPersonalChangeDtoToPo(UserPersonalUpdateDto userPersonalUpdateDto,String id);

    /**
     * 管理员信息修改
     *
     * @param userPersonalUpdateDto
     * @return
     */
    UserPo UserAdministerUpdateDtoToPo(UserAdministerUpdateDto userPersonalUpdateDto);

    /**
     * 转换sex为字符
     *
     * @param sex
     * @return
     */
    default String changeSex(Integer sex) {
        if (sex == 1) {
            return "男";
        }
        if (sex == 2) {
            return "女";
        }
        return "保密";
    }

    /**
     * 转换role为字符
     *
     * @param role
     * @return
     */
    default String changeRole(Integer role) {
        switch (role) {
            case 1:
                return "游客";
            case 2:
                return "用户";
            case 4:
                return "管理员";
            case 6:
                return "超级管理员";
        }

        return "未定义角色";
    }

    /**
     * 转换state为字符
     *
     * @param state
     * @return
     */
    default String changeState(Integer state) {
        if(state==1){
            return "正常";
        }
        if(state==2){
            return "注销";
        }
        if(state==3){
            return "删除";
        }
        return "未知";
    }
}
